package com.iot.ammeter.controller.role;


import com.iot.ammeter.anno.LogAnnotation;
import com.iot.ammeter.dto.web.role.RoleDTO;
import com.iot.ammeter.responseResult.ResponseResult;
import com.iot.ammeter.service.web.role.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author basil
 * @since 2021-09-25
 */
@RestController
@Api(tags = {"角色管理"})
@RequestMapping("/api/role")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 添加角色
     *
     * @param roleDto 角色信息
     * @return 添加成功或者添加失败提示
     */
    @ApiOperation("添加角色")
    @LogAnnotation(module = "addRole", operator = "添加角色")
    @PostMapping("/adjuration")
    public ResponseResult addRole(@RequestBody RoleDTO roleDto) {
        String tips = roleService.addRole(roleDto);
        return "角色添加成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }

    /**
     * 获取角色列表
     *
     * @param roleName 角色名称，可不携带，不携带查询所有
     * @return 角色列表json数据
     */
    @ApiOperation("获取角色列表")
    @LogAnnotation(module = "getRoleList", operator = "获取角色列表")
    @GetMapping("/list")
    public ResponseResult getRoleList(@RequestParam(value = "roleName", required = false) String roleName) {
        List<RoleDTO> roleList = roleService.getRoleList(roleName);
        return roleList.size() > 0 ? ResponseResult.SUCCESS(roleList) : ResponseResult.FAILED("无数据");
    }

    /**
     * 获取角色详细信息
     *
     * @param roleId 角色id
     * @return 角色详细信息json数据
     */
    @ApiOperation("获取角色详细信息")
    @LogAnnotation(module = "getRoleDetailById", operator = "获取角色详细信息")
    @GetMapping("/detail/{roleId}")
    public ResponseResult getRoleDetailById(@PathVariable String roleId) {
        if (StringUtils.isEmpty(roleId)) {
            return ResponseResult.FAILED("角色id不能为空");
        }
        RoleDTO role = roleService.getRoleDetailById(roleId);
        return role != null ? ResponseResult.SUCCESS(role) : ResponseResult.FAILED("角色不存在");
    }

    /**
     * 编辑角色详细信息
     *
     * @param roleId  角色id
     * @param roleDto 角色信息
     * @return 修改成功或者失败提示信息
     */
    @ApiOperation("编辑角色详细信息")
    @LogAnnotation(module = "editRole", operator = "编辑角色详细信息")
    @PutMapping("/update/{roleId}")
    public ResponseResult editRole(@PathVariable String roleId, @RequestBody RoleDTO roleDto) {

        String tips = roleService.editRole(roleId, roleDto);
        return "角色修改成功".equals(tips) ? ResponseResult.SUCCESS(tips) : ResponseResult.FAILED(tips);
    }
}
